﻿using DevExpress.XtraBars;
using PureMVC.Interfaces;
using Rail.Commons;
using Rail.Start.Viewer;
using SuperMap.Data;
using SuperMap.Sample.Data;
using SuperMap.UI;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Rail.Start
{
    public class MainFormMediator : PureMVC.Patterns.Mediator, IMediator
    {
        public new const string NAME = "MainFormMediator";
        FormMain m_FormMain;
        /// <summary>
        /// 场景是否全屏显示
        /// </summary>
        private bool isSceneFullScreenShow = false;
        /// <summary>
        /// 保存场景全屏前工具栏的状态
        /// </summary>
        private bool[] viewState;

        public MainFormMediator(FormMain viewComponent)
            : base(NAME, viewComponent)
        {
            m_FormMain = viewComponent;
            this.CreateMessageCenter(viewComponent);
            //this.CreateScene(viewComponent);
            this.RegisterMenusEvents();
            InitialStoreEvent();
        }
        /// <summary>
        /// 创建消息面板
        /// </summary>
        /// <param name="mainForm"></param>
        private void CreateMessageCenter(FormMain mainForm)
        {
            MesssagePanel mp = new MesssagePanel()
            {
                Dock = System.Windows.Forms.DockStyle.Fill
            };
            mainForm.DockPanelBottom.Controls.Add(mp);
            MessageCenterMediator mcm = new MessageCenterMediator(mp,mainForm.StatusLabel,mainForm.ProgressBar);
            Facade.RegisterMediator(mcm);

        }

        private string CreateScene(FormMain mainForm)
        {
            SceneExtend se = new SceneExtend()
            {
                Dock = System.Windows.Forms.DockStyle.Fill
            };
            mainForm.DockPanelContainerScene.Controls.Add(se);
            SceneExtendMediator sem = new SceneExtendMediator(se);
            Facade.RegisterMediator(sem);
            return se.Scene.ToXML();
        }
        /// <summary>
        /// 注册消息
        /// </summary>
        /// <returns></returns>
        public override IList<string> ListNotificationInterests()
        {
            IList<string> list = new List<string>();
            return list;
        }
        /// <summary>
        /// 处理消息
        /// </summary>
        /// <param name="note"></param>
        public override void HandleNotification(INotification note)
        {
        }
        /// <summary>
        /// 注册菜单的点击事件
        /// </summary>
        void RegisterMenusEvents()
        {
            var rc = m_FormMain.RibbonControl;
            foreach (var item in rc.Items)
            {
                if (item is DevExpress.XtraBars.BarButtonItem)
                {
                    var bbi = item as DevExpress.XtraBars.BarButtonItem;
                    bbi.ItemClick += Bbi_ItemClick;
                }
            }
        }
        /// <summary>
        /// 菜单的点击事件和其Tag相关
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Bbi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string command = e.Item.Tag as string;
            if (string.IsNullOrEmpty(command)) return;
            SendNotification(command,new List<object> 
            {
                this.m_FormMain
            });
        }
        /// <summary>
        /// 按钮事件初始化
        /// </summary>
        private void InitialStoreEvent()
        {
            m_FormMain.BarButtonItemProjectStore.ItemClick += new ItemClickEventHandler(StoreEvent);
            m_FormMain.BarButtonItemProjectClose.ItemClick += new ItemClickEventHandler(CloseEvent);
            m_FormMain.BarBtnItemExit.ItemClick += new ItemClickEventHandler(ExitEvent);
            m_FormMain.BarButtonItemFullScreen.ItemClick += new ItemClickEventHandler(FullScreenEvent);
            m_FormMain.WorkspaceTree.NodeMouseClick += new TreeNodeMouseClickEventHandler(WorkspaceTree_NodeMouseClick);
        }
        /// <summary>
        /// 保存工程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StoreEvent(object sender, ItemClickEventArgs e)
        {
            //WorkspaceConnectionInfo m_connectionInfo = new WorkspaceConnectionInfo();
            //m_connectionInfo.Type = WorkspaceType.SMWU;
            //m_connectionInfo.Server = FormMain.formMain.m_sampleRun.m_connectionInfo.Server;
            FormMain.formMain.m_sampleRun.Save();
        }
        /// <summary>
        /// 关闭工程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CloseEvent(object sender,ItemClickEventArgs e)
        {
            FormMain.formMain.m_sampleRun.Close();
        }  
        /// <summary>
        /// 退出系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ExitEvent(object sender,ItemClickEventArgs e)
        {
            Application.Exit();
        }
        /// <summary>
        /// 全屏事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FullScreenEvent(object sender, ItemClickEventArgs e)
        {
            if (!isSceneFullScreenShow)
            {
                viewState = new bool[5];
                //记录各个部件的显示状态
                viewState[0] = m_FormMain.LeftDockPanel().Visible;
                viewState[1] = m_FormMain.RightDockPanel().Visible;
                viewState[2] = m_FormMain.BottomDockPanel().Visible;
                viewState[4] = m_FormMain.LayerDockPanel().Visible;
                viewState[3] = m_FormMain.DockPanelContainerScene.Visible;
                m_FormMain.LeftDockPanel().Visible =
                m_FormMain.RightDockPanel().Visible =
                m_FormMain.BottomDockPanel().Visible =
                m_FormMain.LayerDockPanel().Visible = false;
                m_FormMain.DockPanelContainerScene.Visible = true;
                isSceneFullScreenShow = true;
                m_FormMain.BarButtonItemFullScreen.Caption = "取消全屏";
            }
            else
            {
                m_FormMain.BottomDockPanel().Visible = viewState[2];
                m_FormMain.LeftDockPanel().Visible = viewState[0];
                m_FormMain.RightDockPanel().Visible = viewState[1];
                m_FormMain.LayerDockPanel().Visible = viewState[4];
                m_FormMain.DockPanelContainerScene.Visible = viewState[3];
                isSceneFullScreenShow = false;
                m_FormMain.BarButtonItemFullScreen.Caption = "全屏";
            }
        }
        /// <summary>
        /// 单击工作空间树节点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WorkspaceTree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            
            //工作空间控件树节点基类，获取树中选中的节点
            TreeNode node = m_FormMain.WorkspaceTree.SelectedNode ;
            if(node==null)
                return;
            //获取选中的节点种类，数据源（矢量，栅格），场景
            string key = node.Name;

            switch (key)
            {
                case "Dataset":
                    {
                       if(e.Button==MouseButtons.Right)
                        {
                            Dictionary<string, Dataset> datasetDic = FormMain.formMain.m_sampleRun.GetDatasetsNames();
                            
                            datasetDic.TryGetValue(node.Text, out dataset);
                            Initcms();
                            cms.Show(m_FormMain.WorkspaceTree, e.X, e.Y);
                        }
                    }
                    break;
                case "Scene":
                    {
                        String sceneName = node.Text;
                        if (m_FormMain.LayersControlpanel.Scene != null)
                        {
                            m_FormMain.LayersControlpanel.Scene.Open(sceneName);
                            FormMain.formMain.m_sampleRun.m_scenecontrol.Refresh();
                        }
                    }
                    break;
            }
        }

        ContextMenuStrip cms;
        Dataset dataset;
        private void Initcms()
        {
            cms = new ContextMenuStrip();

            ToolStripMenuItem tmiEditRoutStation = new ToolStripMenuItem("添加至场景");
            tmiEditRoutStation.Click += new EventHandler(DatasetAddToScene);
            cms.Items.Add(tmiEditRoutStation);

        }
        private void DatasetAddToScene(object sender, EventArgs e)
        {
            FormMain.formMain.m_sampleRun.SceneAddDataset(dataset);
        }
    }
}
